package com.scorekeeper.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;

import com.scorekeeper.objects.Division;
import com.scorekeeper.objects.Team;

public final class Loader {
	private Context context;
	private DBProvider db;
	
	public Loader(Context ctx) { 
		context = ctx;
		db  = new DBProvider(context);
	}
	
	public Map<Integer, Team> getTeams(int divisionId) {
		Team team = null;
		
		Map<Integer, Team> teams = new HashMap<Integer, Team>();
		
		db.open();
		Cursor cur = db.select("SELECT ID FROM Team WHERE DivisionId = ?", new String[] { Integer.toString(divisionId) });
		
		if (db.isValidCursor(cur)) {
			do {
				team = new Team(context);
				team.load(cur.getInt(0));
				
				teams.put(team._id, team);
			} while (cur.moveToNext());
		}
		
		db.close();
		return teams;
	}
	
	public Map<Integer, Team> getTeams() {
		Team team = null;
		Map<Integer, Team> teams = new HashMap<Integer, Team>();
		
		db.open();
		Cursor cur = db.select("SELECT ID FROM Team", null);
		
		if (db.isValidCursor(cur)) {
			do {
				team = new Team(context);
				team.load(cur.getInt(0));
				
				teams.put(team._id, team);
			} while (cur.moveToNext());
		}
		
		db.close();
		return teams;
	}
	
	public ArrayList<String> getTeamNames(int divisionId) {
		ArrayList<String> teams = new ArrayList<String>();
		
		db.open();
		Cursor cur = db.select("SELECT TeamName FROM Team WHERE DivisionId = ?", new String[] { Integer.toString(divisionId) });
		
		if (db.isValidCursor(cur)) {
			do {
				teams.add(cur.getString(0));
			} while (cur.moveToNext());
		}
		
		db.close();
		return teams;
	}
	
	public ArrayList<String> getTeamNames() {
		ArrayList<String> teams = new ArrayList<String>();
		
		db.open();
		Cursor cur = db.select("SELECT TeamName FROM Team", null);
		
		if (db.isValidCursor(cur)) {
			do {
				teams.add(cur.getString(0));
			} while (cur.moveToNext());
		}
		
		db.close();
		return teams;
	}
	
	public ArrayList<String> getDivisionNames() {
		Division division = null;
		ArrayList<String> divisions = new ArrayList<String>();
		
		db.open();
		Cursor cur = db.select("SELECT ID FROM Division", null);
		
		if (db.isValidCursor(cur)) {
			do {
				division = new Division(context);
				division.load(cur.getInt(0));
				
				divisions.add(division.name);
			} while (cur.moveToNext());
		}
		
		db.close();
		return divisions;
	}
	
	public ArrayList<String> getDivisionNames(int conferenceId) {
		ArrayList<String> divisions = new ArrayList<String>();
		
		db.open();
		Cursor cur = db.select("SELECT DivisionName FROM Division WHERE ID IN (?, ?, ?)", 
				new String[] { 
					Integer.toString((conferenceId == 1) ? 1 : 4), 
					Integer.toString((conferenceId == 1) ? 2 : 5), 
					Integer.toString((conferenceId == 1) ? 3 : 6)
				});
		
		if (db.isValidCursor(cur)) {
			do {
				divisions.add(cur.getString(0));
			} while (cur.moveToNext());
		}
		
		db.close();
		return divisions;
	}
	
	public ArrayList<String> getTeamNamesByConference(int conferenceId) {
		ArrayList<String> teams = new ArrayList<String>();
		
		db.open();
		Cursor cur = db.select("SELECT TeamName FROM Team WHERE DivisionId IN (?, ?, ?)", 
				new String[] { 
				Integer.toString((conferenceId == 1) ? 1 : 4), 
				Integer.toString((conferenceId == 1) ? 2 : 5), 
				Integer.toString((conferenceId == 1) ? 3 : 6)
			});
		
		if (db.isValidCursor(cur)) {
			do {
				teams.add(cur.getString(0));
			} while (cur.moveToNext());
		}
		
		db.close();
		return teams;
	}
	
	public Map<Integer, Division> getDivisions() {
		Division division = null;
		Map<Integer, Division> divisions = new HashMap<Integer, Division>();
		
		db.open();
		Cursor cur = db.select("SELECT ID FROM Division", null);
		
		if (db.isValidCursor(cur)) {
			do {
				division = new Division(context);
				division.load(cur.getInt(0));
				
				divisions.put(division._id, division);
			} while (cur.moveToNext());
		}
		
		db.close();
		return divisions;
	}
	
	public int getDivisionFromName(String divisionName) {
		Division div = new Division(context);
		
		div.load(divisionName);
		return div._id;
	}
}
